<template>
  <div class="login_container" ref="vantaRef">
    <div class="imgBox">
      <div class="item" v-for="(url,i) in images">
        <img
          class="img"
          crossorigin="anonymous"
          @mouseenter="handMouseEnter($event.target,i)"
          @mouseleave="handMouseLeave"
          :src="url.img"
          :style="{
          opacity:hoverIndex=== -1 ? 1 : i ===hoverIndex ? 1 : 0.2
        }"
        />
      </div>
    </div>
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <lottieAnim :options="defaultOptions" :height="80" :width="80" />
        <!-- <img src="../assets/logo.png" alt="" /> -->
      </div>
      <!-- 表单 ：model数据绑定 :rules验证规则 ref:添加引用，值就是引用对象，用来重置表单数据-->
      <el-form
        label-width="0px"
        class="login_form"
        :model="login_form"
        :rules="form_rules"
        ref="loginFormRef"
      >
        <!-- 用户名 prop属性：验证规则-->
        <el-form-item prop="username">
          <!-- 输入框放入图标 prefix-icon前置，suffix-icon后置-->
          <!-- v-model绑定表单的数据对象 clearable:是可清除输入框属性-->
          <el-input prefix-icon="el-icon-user-solid" v-model="login_form.username" clearable></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="el-icon-unlock"
            v-model="login_form.password"
            clearable
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>

        <el-form-item prop="status">
          <silder-verify @success="handleSuccess" @failed="handleError"></silder-verify>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" size="mini" @click="login">登录</el-button>
          <el-button type="info" size="mini" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import colorThief from "colorthief";
import SilderVerify from "@/components/SilderVerify";

import lottieAnim from "@/components/lottie/index";
import * as lottieJson from "../assets/lottieJson/129634-firing-and-smiling-gold-coin.json"; //lottie动画json

import * as THREE from "three"; //导入样式
import BIRDS from "vanta/src/vanta.birds"; //导入动态样式逻辑
import GLOBE from "vanta/src/vanta.globe";
import HALO from "vanta/src/vanta.halo";
const ColorThief = new colorThief();

export default {
  components: {
    SilderVerify,
    lottieAnim
  },
  mounted() {
    // this.vantaEffect = BIRDS({
    //   el: this.$refs.vantaRef,
    //   THREE: THREE
    // });
    // 修改颜色时 cells 需要全大写字母 可生效
    // VANTA.BIRDS({
    //   el: this.$refs.vantaRef,
    //   mouseControls: true,
    //   touchControls: true,
    //   gyroControls: false,
    //   minHeight: 200.0,
    //   minWidth: 200.0,
    //   scale: 1.0,
    //   color1: 14381274,
    //   color2: 16443110
    // });
    // this.vantaEffect = HALO({
    //   el: this.$refs.vantaRef,
    //   THREE: THREE
    // });
    // VANTA.HALO({
    //   el: this.$refs.vantaRef,
    //   /*以下为样式配置*/
    //   mouseControls: true,
    //   touchControls: true,
    //   gyroControls: false,
    //   minHeight: 200.0,
    //   minWidth: 200.0,
    //   scale: 1.0,
    //   scaleMobile: 1.0,
    //   color: 0xc5c5c5,
    //   size: 1.1,
    //   backgroundColor: 0x4300bb
    // });
    this.bgDom = document.getElementsByClassName("login_container")[0];
    let img = document.getElementsByClassName("img")[0];
    this.handMouseEnter(img,0)
  },
  beforeDestroy() {
    // if (this.vantaEffect) {
    //   this.vantaEffect.destroy();
    // }
  },
  data() {
    var validatestatus = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请拖动滑块完成验证"));
      } else {
        callback();
      }
    };
    return {
      // 登录表单数据绑定对象
      login_form: {
        username: "",
        password: "",
        status: true
      },
      //   表单验证规则
      form_rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }, // blur：失去焦点触发
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
          // { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ],
        status: [{ validator: validatestatus, trigger: "change" }]
      },
      //lottie配置
      defaultOptions: {
        animationData: lottieJson.default /*文件资源*/
      },
      hoverIndex: -1,
      images: [
        { img: require("@/assets/img/1.jpg") },
        { img: require("@/assets/img/earth.jpeg") },
        { img: require("@/assets/img/favicon.png") },
        { img: require("@/assets/img/kk.gif") }
      ],
      bgDom : "",
    };
  },
  methods: {
    // 点击重置按钮，重置表单
    resetLoginForm() {
      // 重置数据，resetFields（）是重置表单的方法
      this.$refs.loginFormRef.resetFields();
      this.login_form.status = false;
    },
    // 登录
    login() {
      // 通过引用实例对象调用validate方法验证
      this.$refs.loginFormRef.validate(valid => {
        // 返回的是布尔值
        if (!valid) return; // 验证不成功，直接返回
        this.$cookie.set("userName", this.login_form.username);
        this.$store.dispatch("Login", this.login_form).then(() => {
          this.$router.push({ path: "./home" });
        });
      });
    },
    handleSuccess() {
      this.login_form.status = true;
      // this.$refs.loginFormRef.validateField("status");
      console.log("成功");
    },
    handleError() {
      // this.$refs.loginFormRef.validateField("status");
      console.log("失败");
      this.login_form.status = false;
    },
    // 图片鼠标移入
    async handMouseEnter(img, i) {
      this.hoverIndex = i;
      //第一个参数：传入图片元素，第二个元素：需要提前几种颜色
      const color = await ColorThief.getPalette(img, 3);
      const [c1, c2, c3] = color.map(c => `rgb(${c[1]},${c[1]},${c[2]})`);
      this.bgDom.style.background = `linear-gradient(${c1} 0%,${c2} 50%,${c3} 100%`;
    },
    // 图片鼠标移出
    handMouseLeave() {
      this.hoverIndex = -1;
      // this.bgDom.style.background = `#fff`;
    }
  }
};
</script>

<style  scoped>
.login_container {
  height: 98vh;
  /* background: #2b4b6b; */
}
.login_box {
  /* background: #2b4b6b; */
  width: 400px;
  height: 330px;
  /* background-color: #fff; */
  border-radius: 3px;
  position: absolute;
  left: 80%;
  top: 50%;
  /* 相对自己移动 */
  transform: translate(-50%, -50%);
  border: 1px solid #fff;
}
.avatar_box {
  height: 80px;
  width: 80px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
}
.avatar_box > img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}
/* 按钮放到右边 */
.btns {
  display: flex;
  justify-content: flex-end;
}
/* 表单区域 */
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  /* 设置边框属性，让它不会超出 */
  box-sizing: border-box;
}
.item {
  width: 100px;
  height: 100px;
  margin: 2px;
}
.item img {
  width: 100%;
  height: 100%;
}
.imgBox{
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
}
</style>
